<?php

class yzBinder extends yzCore{

    protected $aProperties = array(
        'yz.binder.table.name'  => '',
        'yz.binder.field1'      => '',
        'yz.binder.field2'      => '',
        'yz.binder.select'      => '*'
    ); // array

    protected $aErrors = array(
        901 => 'No yzDatabase module',

    );

    public function init(){

    	$this->setObject('yzSql');

    	if (!$this->isSetObject('yzDatabase') && !$this->setObject('yzDatabase')){

    		throw new yzException($this->aErrors[901]);

    		return false;

    	}

        if ($this->yzBinderSelect==null){

        	$this->yzBinderSelect=implode(',',$this->getProperties('yz.binder.field*'));

        } // if

    }

    public function bind($mValue1, $mValue2){

    	$sQuery = $this->yzSql->replace($this->yzBinderTableName,array($this->yzBinderField1=>$mValue1,$this->yzBinderField2=>$mValue2));

    	$this->yzDatabase->query($sQuery);

    	return $this->yzDatabase->errorInfo();

    }

    public function update($mValue1, $mValue2){

        $sQuery = $this->yzSql->update($this->yzBinderTableName,array($this->yzBinderField2=>$mValue2),array($this->yzBinderField1=>$mValue1));

        $this->yzDatabase->query($sQuery);

        return $this->yzDatabase->errorInfo();

    }

    public function unbind($mValue1, $mValue2=null){

        $sQuery = $this->yzSql->delete($this->yzBinderTableName,array($this->yzBinderField1=>$mValue1,$this->yzBinderField2=>$mValue2));

        $this->yzDatabase->query($sQuery);

        return $this->yzDatabase->errorInfo();

    }

    public function get($mValue = null, $mOrder = null){

        $sQuery =  $this->yzSql->select($this->yzBinderSelect, $this->yzBinderTableName, ($mValue==null) ? null : array($this->yzBinderField1=>$mValue),$mOrder);

        $this->yzDatabase->query($sQuery);

        return $this->yzDatabase->errorInfo();

    } //get

    public function reset($mValue, $mDefaultValue=null){

    	$sQuery =  $this->yzSql->update($this->yzBinderTableName,array($this->yzBinderField1=>$mDefaultValue),array($this->yzBinderField1=>$mValue));

    	$this->yzDatabase->query($sQuery);

        return $this->yzDatabase->errorInfo();

    } // reset

    public function remove($mValue){

    	$sQuery =  $this->yzSql->delete($this->yzBinderTableName,array($this->yzBinderField1=>$mValue));

        $this->yzDatabase->query($sQuery);

        return $this->yzDatabase->errorInfo();

    } // remove

    public function bindTo($mValue, $aValues){

    	foreach ($aValues as $mDataValue){

    		$aData[] = array($mValue,$mDataValue);

    	} // foreach

        $sQuery =  $this->yzSql->replace($this->yzBinderTableName,array($this->yzBinderField1,$this->yzBinderField2),$aData);

        $this->yzDatabase->query($sQuery);

        return $this->yzDatabase->errorInfo();


    } // bindTo


}


?>